#include "tos_k.h"

#define STK_SIZE_TASK_DEMO      512

k_stack_t stack_task_demo[STK_SIZE_TASK_DEMO];

k_task_t task_demo;

extern void entry_task_demo(void *arg);

// 创建一个表示灯状态的枚举
typedef enum {
    LIGHT_OFF = 0,
    LIGHT_ON
} light_state_t;

light_state_t light_state = LIGHT_OFF;

// 对灯的状态进行翻转
void light_control_cb(void *arg)
{
    if (light_state == LIGHT_OFF) {
        light_state = LIGHT_ON;
        printf("Light turned ON, current systick: %lld\n", tos_systick_get());
    } else {
        light_state = LIGHT_OFF;
        printf("Light turned OFF, current systick: %lld\n", tos_systick_get());
    }
}

// 检查灯的状态
void light_status_check_cb(void *arg)
{
    if (light_state == LIGHT_ON) {
        printf("Light is ON, current systick: %lld\n", tos_systick_get());
    } else {
        printf("Light is OFF, current systick: %lld\n", tos_systick_get());
    }
}

// 将灯的状态设为开
void light_turn_on_cb(void *arg)
{
    light_state = LIGHT_ON;
    printf("Light turned ON by oneshot timer, current systick: %lld\n", tos_systick_get());
}

void entry_task_demo(void *arg)
{
    k_timer_t light_control_tmr;
    k_timer_t light_status_check_tmr;
    k_timer_t light_turn_on_tmr;

    // 创建一个周期性的定时器，每5000个tick切换一次灯的状态（开/关）
    tos_timer_create(&light_control_tmr, 5000, 5000, light_control_cb, K_NULL, TOS_OPT_TIMER_PERIODIC);
    // 创建一个周期性的定时器，每2000个tick检查一次灯的状态
    tos_timer_create(&light_status_check_tmr, 2000, 2000, light_status_check_cb, K_NULL, TOS_OPT_TIMER_PERIODIC);
    // 创建一个一次性的定时器，在第1000个tick时将灯打开
    tos_timer_create(&light_turn_on_tmr, 1000, 0, light_turn_on_cb, K_NULL, TOS_OPT_TIMER_ONESHOT);

    printf("current systick: %lld\n", tos_systick_get());
    tos_timer_start(&light_control_tmr);
    tos_timer_start(&light_status_check_tmr);
    tos_timer_start(&light_turn_on_tmr);

    while (K_TRUE) {
        tos_task_delay(1000);
    }
}

int main(void)
{
    tos_knl_init();
    (void)tos_task_create(&task_demo, "task_demo", entry_task_demo, NULL,
                            4, stack_task_demo, STK_SIZE_TASK_DEMO, 0);
    tos_knl_start();
}
